package com.wang.tool.executor;

import com.wang.tool.executor.transitivethreadcontext.ThreadContextTransferTool;
import com.wang.tool.executor.transitivethreadcontext.ThreadContextTransmitter;
import com.wang.tool.executor.transitivethreadcontext.TransitiveExecutorService;
import com.wang.tool.hook.AppHook;
import com.wang.tool.invoker.ConcurrentInvoker;
import com.wang.tool.monitor.core.LinkHolder;
import com.wang.tool.monitor.core.LinkInfo;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

class Demo {

    public static void main(String[] args) {
//          全局统一的线程池管理，全局配置线程上下文切换
//          简单易用&支持动态配置
//          延伸：ThreadContextTransferTool
//          可对接AppHook


//        1.构建线程池
        ThreadPoolExecutor source = new ThreadPoolExecutor(10,20,30, TimeUnit.SECONDS,new LinkedBlockingQueue<>(10));

//        1.（简易ttl）线程上下文切换配置，多线程处理时线程切换动作，这里是表示自动切换链路信息
//        全局配置，仅在初始化时配置一次即可
        ThreadContextTransferTool.addGlobalTransmitter(new ThreadContextTransmitter() {
            @Override
            public Object get() {
                return LinkHolder.getCurrent();
            }

            @Override
            public void set(Object obj) {
                if (obj==null) {
                    LinkHolder.clear();
                }else{
                    LinkHolder.setCurrent((LinkInfo) obj);
                }
            }
        });

//        1.构建包装线程池
        TransitiveExecutorService wrap = ThreadContextTransferTool.wrap(source);
//        2.注册线程池
        ExecutorFactory.register("test", source, wrap);


//        3.使用线程池
        ExecutorService test = ExecutorFactory.getExecutor("test");

        ArrayList<Runnable> objects = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            objects.add(new Runnable() {
                @Override
                public void run() {
                    System.out.println("11");
                }
            });
        }

//        3.并行调用
        ConcurrentInvoker.batchRun("测试任务", test, 100, objects, new BiConsumer<Integer, Throwable>() {
            @Override
            public void accept(Integer integer, Throwable throwable) {
                System.out.println("出错啦");
            }
        });



//        3.动态配置线程配置，可对接配置中心
        ExecutorFactory.changeSize("test", 5, 10);


//        高阶用法，配置AppHook 在程序启动时初始化线程池，结束时回收线程池








    }

    private static class ExecutorInit implements AppHook {
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            //配置线程池切换器
            ThreadContextTransferTool.addGlobalTransmitter(new ThreadContextTransmitter() {
                @Override
                public Object get() {
                    return LinkHolder.getCurrent();
                }

                @Override
                public void set(Object obj) {
                    if (obj==null) {
                        LinkHolder.clear();
                    }else{
                        LinkHolder.setCurrent((LinkInfo) obj);
                    }
                }
            });



            for (ExeEnum value : ExeEnum.values()) {

                //根据枚举配置初始化线程池
                ThreadPoolExecutor source = null;
                ExecutorService wrap = ThreadContextTransferTool.wrap(source);;
                //注册
                ExecutorFactory.register(value.name(),source,wrap);


            }

        }

        @Override
        public void destroy() throws Exception {
            for (ExeEnum value : ExeEnum.values()) {

//                回收


            }
        }
    }


    private static enum ExeEnum{
        test;

    }

}